package org.zp.handler;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zp.dto.Getrolelist;
import org.zp.pojo.AdminInfo;
import org.zp.pojo.RoleInfo;
import org.zp.service.AdminInfoService;
import org.zp.service.AdminLogInfoService;
import org.zp.service.RMService;
import org.zp.service.RoleInfoService;
import org.zp.util.Ajax;

import com.github.pagehelper.PageInfo;
/**
 * 权限业务
 * @FileName AccessHandler.java
 * @author zp
 * @package 2020年8月6日
 * @TODO
 */
@RestController
public class AccessHandler {
	@Resource
	private RoleInfoService r;
	@Resource
	private RMService rm;
	@Resource
	private AdminInfoService a;
	@Resource
	private AdminLogInfoService log;
	
	private String url = "角色列表";
	/**
	 * 角色多条件查询
	 * @param dto
	 * @param req
	 * @param resp
	 */
	@GetMapping("getrolelist.act")
	public void getrolelist(Getrolelist dto,HttpServletRequest req, HttpServletResponse resp) {
		Integer startpagee = Integer.parseInt(dto.getStartpage());
		Integer limitpagee = Integer.parseInt(dto.getLimitpage());
		String name = dto.getRoleName();
		String start = dto.getStart();
		String end = dto.getEnd();
		
		if (name.equals("")) {
			name = null;
		}
		
		if (start.equals("")) {
			start = null;
		}
		if (end.equals("")) {
			end = null;
		}
		if (start != null && end != null) {
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 日期格式处理
			try {
				Date star = df.parse(start);
				Date endd = df.parse(end);
				if (star.getTime() > endd.getTime()) {// 比较时间的大小
					Ajax.error(resp, "时间有误", 10001);
					return;
				}
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		List<RoleInfo> list = r.selectRole(startpagee, limitpagee, name,start, end);
		PageInfo<RoleInfo> pageInfo = new PageInfo<RoleInfo>(list);
		Integer count = (int) pageInfo.getTotal();
		System.out.println("count:" + count);
		Ajax.success(resp, "list", "count", list, count);
		
	}
	/**
	 * 添加新角色
	 * @param roleName
	 * @param state
	 * @param req
	 * @param resp
	 */
	@PostMapping("roleAdd.act")
	public void roleAdd(String roleName, String state,HttpServletRequest req, HttpServletResponse resp) {
		int iStatus = 0;

		try {
			iStatus = Integer.parseInt(state);
		} catch (NumberFormatException e) {
			Ajax.error(resp, "状态错误", 10000);
			e.printStackTrace();
			return;
		}
		Integer index = r.roleAdd(roleName,iStatus);
		if (index > 0) {
			Ajax.success(resp);
			System.out.println("添加成功");
			AdminInfo admin1 = (AdminInfo) req.getSession().getAttribute("admin");
			Long aid1 = admin1.getAdminId();
			Integer cid = 1;//此为添加的操作id
			Integer logs = log.addlog(aid1,cid,url);
			if (logs>0) {
				System.out.println("加入日志成功");
			}else {
				System.out.println("加入日志失败");
				return;
			}
			return;
		} else {
			Ajax.error(resp, "添加失败", 10001);
			System.out.println("添加失败");
		}
	}
/**
 * 添加权限
 * @param rid
 * @param req
 * @param resp
 */
	@PostMapping("amendRoleJur.act")
	public void amendRoleJur(String rid,
			HttpServletRequest req, HttpServletResponse resp) {
		System.out.println("rid:"+rid);
		Integer ridd = Integer.parseInt(rid);
		String []arr = req.getParameterValues("arr[]");
		for (int i = 0; i < arr.length; i++) {
			System.out.println(arr[i]);
		}
		Integer flag = rm.saveAccess(ridd,arr);
		if(flag>0) {
			System.out.println("保存成功");
			AdminInfo admin1 = (AdminInfo) req.getSession().getAttribute("admin");
			Long aid1 = admin1.getAdminId();
			Integer cid = 1;//此为添加的操作id
			Integer logs = log.addlog(aid1,cid,url);
			if (logs>0) {
				System.out.println("加入日志成功");
			}else {
				System.out.println("加入日志失败");
				return;
			}
			Ajax.success(resp);
			}else {
			Ajax.error(resp,"保存失败");
			}
	}
	
	@PostMapping("roleAmendState.act")
	public void roleAmendState(String id,String state, HttpServletRequest req, HttpServletResponse resp) {
		Integer Id = 0;
		Integer State = 0;
		try {
			Id = Integer.parseInt(id);
			State = Integer.parseInt(state);
		} catch (Exception e) {
			// TODO: handle exception
		}
		Integer x = r.updateState(Id,State);
		if (x>0) {
			Ajax.success(resp);
			System.out.println("修改成功");
			AdminInfo admin1 = (AdminInfo) req.getSession().getAttribute("admin");
			Long aid1 = admin1.getAdminId();
			Integer cid = 3;//此为改的操作id
			Integer logs = log.addlog(aid1,cid,url);
			if (logs>0) {
				System.out.println("加入日志成功");
			}else {
				System.out.println("加入日志失败");
				return;
			}
			return;
		}else {
			Ajax.error(resp, "修改失败",10001);
			System.out.println("修改失败");
		}
	}
	/**
	 * 获得角色集合
	 * @param req
	 * @param resp
	 */
	@GetMapping("getrole.act")
	public void getrole(HttpServletRequest req, HttpServletResponse resp) {
		List<RoleInfo> list = r.showRole();
		Ajax.success(resp, "list", list);
	}
	/**
	 * 获得该角色以下的角色
	 * @param req
	 * @param resp
	 */
	@GetMapping("getrolewithaccess.act")
	public void getrolewithaccess(HttpServletRequest req, HttpServletResponse resp) {
		//先获得该管理员的对象
		AdminInfo admin = (AdminInfo) req.getSession().getAttribute("admin");
		//获得该管理员的角色id
		Long rid = admin.getRoleId();
		
		List<RoleInfo> list = r.getrolewithaccess(rid);
		
		Ajax.success(resp, "list", list);
		return;
		
	}
	/**
	 * 获得当前登录者的角色id
	 * @param req
	 * @param resp
	 */
	@GetMapping("getAdminRid.act")
	public void getUsername(HttpServletRequest req, HttpServletResponse resp) {
		AdminInfo admin = (AdminInfo) req.getSession().getAttribute("admin");
		Long aid = admin.getAdminId();
		AdminInfo aInfo = a.getAdminByAid(aid);
		Long rid = aInfo.getRoleId();
		System.out.println("-----------------------------登陆者角色id:"+rid);
		Ajax.success(resp, "rid", rid);
	}
	
}
